package com.atguigu.gmall.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.CategoryMapper;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import com.atguigu.gmall.pms.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements CategoryService {

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<CategoryEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<CategoryEntity> parent(String parentId) {
        LambdaQueryWrapper<CategoryEntity> queryWrapper
                 = new LambdaQueryWrapper<>();
        if(!StringUtils.equals(parentId, "-1")){//如果parentId=-1代表查询所有 否则当做parentId参数值查询
            queryWrapper.eq(CategoryEntity::getParentId , parentId);
        }
        return  this.list(queryWrapper);
    }

    @Override
    public List<CategoryEntity> lv2AndSubsCates(String cid) {

        return baseMapper.selectLv2AndSubsCates(cid);
    }

    @Override
    public List<CategoryEntity> queryLevel123Cates(String c3id) {
        //查询三级分类
        CategoryEntity c3 = this.getById(c3id);
        //根据三级分类的pid查询二级分类
        Long c2id = c3.getParentId();
        CategoryEntity c2 = this.getById(c2id);
        //根据二级分类的pid查询1级分类
        Long c1id = c2.getParentId();
        CategoryEntity c1 = this.getById(c1id);

        return Arrays.asList(c1,c2,c3);
    }

}